/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.Org;
import com.apache.uct.manager.OrgManager;

/**
 * description:  机构信息保存
 * @author Hou Dayu 创建时间：2015-8-5  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class OrgSavePlugin implements PluginConnector {

	private OrgManager orgManager;

	/**
	 * TODO 机构信息保存.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String orgEname = String.valueOf(vo.getParams("orgEname"));
		String orgCname = String.valueOf(vo.getParams("orgCname"));

		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(orgEname) || Validator.isNull(orgCname)) {
			entity.setMessage("机构代码与机构名称不可为空");
			entity.setEntity("");
		} else {
			Org org = new Org();
			org = (Org) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), org);
			ParamsVo<Org> pvo = new ParamsVo<Org>();
			pvo.setObj(org);

			String orgId = String.valueOf(vo.getParams("orgId"));
			if (Validator.isNotNull(orgId)) {
				orgManager.editInfo(pvo);
			} else {
				orgId = orgManager.saveInfo(pvo);
			}
			entity.setEntity("");
			if (Validator.isNotNull(orgId)) {
				entity.setMessage("机构" + org.getOrgCname() + "添加成功");
				entity.setEntity(orgId);
			} else {
				entity.setMessage("机构" + org.getOrgCname() + "添加失败");
			}
		}
		entity.setResult("true");
		return entity;
	}

	//组装用户信息
	private Org buildOrgInfo(ParamsVo vo, Org org) {
		org.setFatherId(Validator.getDefaultStr(String.valueOf(vo.getParams("fatherId")), "0"));
		org.setOrgCode(getParamStr(vo, "orgCode"));
		org.setAddress(getParamStr(vo, "address"));
		org.setBoss(getParamStr(vo, "boss"));
		org.setCity(getParamStr(vo, "city"));
		org.setCreateUser(getParamStr(vo, "createUser"));
		org.setEmail(getParamStr(vo, "email"));
		org.setFax(getParamStr(vo, "fax"));
		org.setHomepage(getParamStr(vo, "homepage"));
		org.setLinkMan(getParamStr(vo, "linkMan"));
		org.setMobile(getParamStr(vo, "linkMan"));
		org.setOrgRegMoney(getParamStr(vo, "orgRegMoney"));
		org.setOrgRemark(getParamStr(vo, "orgRemark"));
		org.setOrgScope(getParamStr(vo, "orgScope"));
		org.setOrgStatus(1);
		org.setOrgType(getParamStr(vo, "orgType"));
		org.setPhone(getParamStr(vo, "phone"));
		org.setProvince(getParamStr(vo, "province"));
		org.setPostCode(getParamStr(vo, "postCode"));
		org.setCreateTime(System.currentTimeMillis());
		org.setRegTime(org.getCreateTime());
		org.setSysEname(getParamStr(vo, "sysEname"));
		org.setUpdateCount(0);
		org.setSubCount(0);
		org.setFullEname("/" + org.getOrgEname());
		org.setFullCname("/" + org.getOrgCname());
		if (!"0".equals(org.getFatherId())) {
			ParamsVo<Org> pvo = new ParamsVo<Org>();
			pvo.setInfoId(org.getFatherId());
			Org porg = (Org) orgManager.getInfoById(pvo);
			if (!Validator.isEmpty(porg)) {
				org.setFullEname(porg.getFullEname() + "/" + org.getOrgEname());
				org.setFullCname(porg.getFullCname() + "/" + org.getOrgCname());
			}
		}
		return org;
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

}
